package com.naiterui.ehp.bs.cms.modules.agent.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.advice.IgnoreResponseEntityWrap;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.utils.FileUtil;
import com.naiterui.ehp.bs.cms.common.controller.AbstractController;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.agent.po.FindCustomPO;
import com.naiterui.ehp.bs.cms.modules.agent.service.ICustomService;
import com.naiterui.ehp.bs.cms.modules.agent.vo.CustomPageItemVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author yiren
 * @since 2022/2/10
 */
@Slf4j
@Api(tags = {SwaggerTag.CMS + SwaggerTag.AGENT_MANGEN})
@AllArgsConstructor
@RestController
@RequestMapping("/agent/custom")
@PreAuthorize("@p.check('agent:custom')")
public class AgentCustomController extends AbstractController {

	private final ICustomService customService;

	@SysLog("查询客户数据")
	@GetMapping("/find")
	@ApiOperation("查询客户数据")
    // @PreAuthorize("@p.check('agent:custom:find')")
	public PageVO<CustomPageItemVO> findCustom(PageParamsVO pageParamsVO, FindCustomPO customPO) throws BusinessException {
		return customService.queryPage(pageParamsVO, customPO);
	}

    @GetMapping("/export")
    @ApiOperation("医生数据导出")
    // @PreAuthorize("@p.check('agent:custom:export')")
    @IgnoreResponseEntityWrap
    @ApiImplicitParams({
        @ApiImplicitParam(name = "doctorIds", value = "医生id，多个以逗号分割", dataType = "string", paramType = "query",
            required = true),
        @ApiImplicitParam(name = "startDate", value = "开始时间，yyyy-MM-dd", dataType = "string", paramType = "query",
            required = true),
        @ApiImplicitParam(name = "endDate", value = "结束时间，yyyy-MM-dd", dataType = "string", paramType = "query")})
    public void customExport(String doctorIds, String startDate, String endDate, HttpServletResponse response)
        throws IOException {
        log.info("客户管理-医生数据导出, doctorIds: {}, startDate: {}, endDate: {}", doctorIds, startDate, endDate);
        byte[] body = customService.customExport(doctorIds, startDate, endDate);
        String fileName = System.getProperty("java.io.tmpdir") + File.separator + System.currentTimeMillis() + ".xls";
        FileUtil.downloadFile(this.request, response, FileUtil.writeBytes(body, fileName), true);
    }

}
